package com.apestech.api.workflow.lib.app;

import com.apestech.api.workflow.lib.FlowUtil;
import com.apestech.api.workflow.lib.element.FlowTask;
import com.apestech.api.workflow.lib.element.WorkProcessEngine;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;

import java.util.ArrayList;

/**
 * 任务结点相关查询
 *
 * @author hup
 * @create 2018-07-21-14:41
 */
public class TaskQueryApp extends FlowUtil{
    private WorkProcessEngine engine;
    private JSONObject input;
    private String processInstanceId;
    private String taskId;
    private FlowTask flowTask;

    public TaskQueryApp(WorkProcessEngine engine, JSONObject input) throws Exception {
        this.engine=engine;
        this.input=input;
        this.processInstanceId=input.getString("processInstanceId");
        this.taskId=input.getString("taskId");
        if(this.processInstanceId==null && this.taskId==null){
            throw new Exception("流程ID或任务ID不允许为空！");
        }
        flowTask= getFlowTask(engine, this.input);
        this.processInstanceId=flowTask.getProcessInstanceId();
    }

    public FlowTask getFlowTask() {
        return flowTask;
    }

    /**
     * 查询结点路由信息
     * @return
     * @throws Exception
     */
    public JSONArray getDecision() throws Exception {
        return flowTask.getNodelist();
    }

    /**
     * 查询当前单据OA结点状态
     * @return
     * @throws Exception
     */
    public String getNodeState() throws Exception {
        return flowTask.getState();
    }

    /**
     * 获取流程中正在运行的结点里所有候选人
     * @return
     * @throws Exception
     */
    public ArrayList<String> getTaskUserList() throws Exception {
        return getTaskUserList(engine, flowTask.getTask().getId());
    }

    /**
     * 获取某个结点下的变量信息
     * @return
     * @throws Exception
     */
    public JSONObject getTaskMap() throws Exception {
        return getTaskMap(engine, flowTask.getTask().getId(), input);
    }

}
